<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增obu发布')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-obupublish-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布名称：</label>
                <div class="col-sm-8">
                    <input name="obuPublishName" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="obuPublishType" class="form-control m-b" id="type" onchange="start()">
                        <option value="蓝牙">蓝牙</option>
                        <option value="OBU">OBU</option>
                    </select>

                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布的版本：</label>
                <div class="col-sm-8">
                    <select name="obuPublishVersion" class="form-control m-b"  id="ele"  onchange="xuanze()">

                    </select>

                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布时间：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="obuPublishTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">发布的url：</label>
                <div class="col-sm-8">
                    <input name="obuPublishUrl" class="form-control" id="url"  type="text" readonly="readonly">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">厂家：</label>
                <div class="col-sm-8">
                    <select name="obuPublishFactory" class="form-control m-b" id="factory">

                    </select>

                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">允许升级的账号(多个用,号隔开)：</label>
                <div class="col-sm-8">
                    <input name="obuUpgradeAccount" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">允许升级版本(多个用,号隔开)：</label>
                <div class="col-sm-8">
                    <input name="obuAllowUpgradeVersion" class="form-control" type="text">
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "obupublish/obupublish"
        $("#form-obupublish-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-obupublish-add').serialize());
            }
        }

        $("input[name='obuPublishTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        var datas;

        function start() {
            //获取被选中的option标签
            var vs = $('#type  option:selected').val();
            console.log(vs);
            $.ajax({
                type: "POST",
                url: "/bluetooth-server/obufile/obuupdata/factorylist",
                data: {
                    obufileFactory: vs
                },
                success: function (data) {
                    console.log(vs);
                    var vaule = data.total;

                    if (vaule > 0) {
                        datas = data.rows;

                        var optionstring = "";
                        for (var j = 0; j < datas.length; j++) {
                            console.log(datas[j].obufileVersion);
                            optionstring += "<option value=\"" + datas[j].obufileVersion + "\" >" + datas[j].obufileVersion + "</option>";
                        }

                        $("#ele").html(optionstring);
                        var datac = datas[0].obufileAddress;
                        $("#url").val(datac);
                    } else {
                        alert("暂无文件选择，请先上传文件！")
                        $("#url").val("");
                        $("#ele").html("<option value=''>暂无</option>");

                    }
                }
            });
        }



        function changjia() {


            $.ajax({
                type: "POST",
                url: "/bluetooth-server/ble_userinfo/ble_userinfo/listforfactory",
                success: function (data) {
                    console.log(data);
                    var vaule = data.total;

                    if (vaule > 0) {
                        datas = data.rows;

                        var optionstring = "";
                        for (var j = 0; j < datas.length; j++) {
                            console.log(datas[j]);
                            optionstring += "<option value=\"" + datas[j] + "\" >" + datas[j] + "</option>";
                        }

                        $("#factory").html(optionstring);


                    } else {
                        alert("暂无文件选择，请先上传文件！")

                        $("#factory").html("<option value=''>暂无</option>");

                    }
                }
            });
        }


        function xuanze() {
            var index = $('#ele').prop('selectedIndex');
            console.log("选择！"+index)
            var datac = datas[index].obufileAddress;
            console.log("选择2！"+datac)
            $("#url").val(datac);
        }

        $(function(){
            start();
            changjia();
        });
    </script>
</body>
</html>